<template>
  <div class="app-container" style="margin-top: -20px;">

    <el-row :gutter="10" class="mb8">
      <right-toolbar :search="search" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table ref="tables" v-loading="loading" :data="list">
      <el-table-column label="用户名称" align="center" prop="userName"
      />
      <el-table-column label="IP" align="center" prop="ipaddr" width="130" :show-overflow-tooltip="true"/>
      <el-table-column label="登录地点" align="center" prop="loginLocation" :show-overflow-tooltip="true"/>
      <el-table-column label="浏览器" align="center" prop="browser" :show-overflow-tooltip="true"/>
      <el-table-column label="操作系统" align="center" prop="os"/>
      <el-table-column label="客户端渠道" align="center" prop="agent">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_branch" :value="String(scope.row.agent)"/>
        </template>
      </el-table-column>
      <el-table-column label="客户端版本" align="center" prop="version"/>
      <el-table-column label="手机型号" align="center" prop="model"/>
      <el-table-column label="设备唯一标识" align="center" prop="deviceToken"/>
      <el-table-column label="操作信息" align="center" prop="msg"/>
      <el-table-column label="登录日期" align="center" prop="loginTime" sortable="custom"
                       :sort-orders="['descending', 'ascending']" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.loginTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="登录状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_common_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-dropdown size="mini" @command="(command) => handleCommand(command, scope.row)">
            <el-button size="mini" type="text" icon="el-icon-d-arrow-right">更多</el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="bannedIP" icon="el-icon-remove"
                                v-hasPermi="['system:appUser:bannedIP']">封禁IP
              </el-dropdown-item>
              <el-dropdown-item command="bannedDevice" icon="el-icon-remove"
                                v-hasPermi="['system:appUser:bannedDevice']">封禁设备
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="封禁" :visible.sync="openBanned" width="500px" append-to-body>
      <el-form :inline="true" :model="bannedBo" class="demo-form-inline">
        <el-form-item label="封禁原因">
          <el-input type="textarea" v-model="bannedBo.reason" placeholder="封禁原因"
                    style="width: 217px;"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="toBanned">确 定</el-button>
        <el-button @click="openBanned = false">取 消</el-button>
      </div>
    </el-dialog>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>
  </div>
</template>

<script>
import {
  banned,
  list,
} from "@/api/monitor/logininfor";

export default {
  name: "LogininforQuery",
  dicts: ['sys_common_status', 'sys_branch', 'limit_type'],
  props: {
    phone: {
      type: String,
      default: '-1'
    },
    userId: {
      type: String,
      default: '-1'
    }
  },
  data() {
    return {
      search: false,
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        userName: this.phone
      },
      // 按钮loading
      buttonLoading: false,
      //封禁IP和设备的表单参数
      bannedBo:{userId: this.userId},
      //封禁IP和设备的表单显示控制
      openBanned:false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询登录日志列表 */
    getList() {
      this.loading = true;
      list(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //更多操作
    handleCommand(command, row) {
      this.openBanned = true;
      this.bannedBo.platformId = 0;
      this.bannedBo.limitType = 99;
      switch (command) {
        case "bannedIP":
          this.bannedBo.target = row.ipaddr;
          this.bannedBo.bannedType = 0;
          break;
        case "bannedDevice":
          this.bannedBo.target = row.deviceToken;
          this.bannedBo.bannedType = 1;
          break;
        default:
          break;
      }
    },
    // 封IP和设备
    toBanned() {
      banned(this.bannedBo).then(response => {
        this.getList();
        this.bannedBo = {userId: this.userId};
        this.openBanned = false
      });
    },
  }
};
</script>
